import React from 'react'
import {Input ,Radio ,Select ,Button ,message } from 'antd';
import './index.less';
import axios from 'axios';
import {PATH,IP} from '../../../utils/urls';
import BreadcrumbCustom from "../../../components/BreadcrumbCustom";
const { Option } = Select;
const infoTitle = ["用户名","账号","手机号","邮箱","角色"];
export default class Profilemanager extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            selfInfoArray:[]//显示的个人信息
        };
    }

    componentDidMount() {
        //获取个人信息
        this.getSelfMessage();
    }

    /**
     * 获取任务信息
     * infoTitle = ["用户名","账号","手机号","邮箱","角色"];
     * @returns {*}
     */
    getSelfMessage(){
        axios.get(PATH.profile,{})
            .then(response=>{
                console.log(response);
                if(response.data.code == 0){
                    let infoObj = response.data.data.user;
                    let infoList = [infoObj.userName,infoObj.loginName,infoObj.phonenumber,infoObj.email,infoObj.remark];
                    for(let i = 0;i<infoList.length;i++){
                        if(i == 1 || i == 4){
                            this.state.selfInfoArray.push({
                                type:infoTitle[i],
                                value:infoList[i],
                                disabled:true
                            })
                        }else{
                            this.state.selfInfoArray.push({
                                type:infoTitle[i],
                                value:infoList[i],
                                disabled:false
                            })
                        }
                    }
                    this.setState({
                        selfInfoArray:this.state.selfInfoArray
                    })
                }
            }
    )}

    /**
     * 修改个人信息
     * @returns {*}
     */
    onSubmit=()=>{
        console.log(this.state);
        let sendData = {
            userName:this.state.selfInfoArray[0].value,
            loginName:this.state.selfInfoArray[1].value,
            phonenumber:this.state.selfInfoArray[2].value,
            email:this.state.selfInfoArray[3].value
        };
        axios.post(PATH.editProfile,sendData).then(response=>{
            console.log(response);
            if(response.data.code == 0){
                message.success(response.data.msg);
                this.getSelfMessage();
            }else{
                message.error(response.data.msg);
            }
        })
    }

    /**
     * 修改密码
     */
    onChangePassWord=()=>{
        let path = {
            pathname:"changepassword"
        }
        this.props.history.push(path);
    }

    /**
     * 输入框值变化
     * @returns {*}
     */
    inputChange = (event)=>{
        console.log(event.target.id);
        if(event && event.target && event.target.value){
            let index = event.target.id.substring(event.target.id.length - 1,event.target.length);
            this.state.selfInfoArray[index].value = event.target.value;
        }
    }

    render() {
        return (
            <div>
                <BreadcrumbCustom first="系统设置" second="个人信息" />
                <div className="changeAllContent">
                    {
                        this.state.selfInfoArray.map((item,i)=>(
                            <div className="resourceContentCell" key={i}>
                                <div className="changeTitle">{item.type}:</div>
                                <Input id={`"editEquipMentInput"${i}`} className="changeCell" defaultValue={item.value} disabled={item.disabled} onChange={event=>this.inputChange(event)}/>
                            </div>
                        ))
                    }
                    <div className="resourceContentCell changePWDiv">
                        <Button className="changePWBtn" onClick={this.onSubmit}>修改</Button>
                        <Button className="changePWBtn" onClick={this.onChangePassWord}>修改密码</Button>
                    </div>
                </div>
            </div>
        )
    }
}